<cfcomponent extends="AbsDAO" output="false">

	<cffunction name="create" access="public" returntype="Numeric" output="false">
		<cfargument name="user" type="it.millemultimedia.easyBlog.model.User" required="true" />	
		<cfset var qry = "" />	
		<cfset var id = 0 />
		
		<cftransaction>
		
			<cftry>
		
				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					INSERT INTO "User"
					(
						status,username,password,email,
					uuid,name,shortdescription,description,editable
					)
					VALUES
					(
						<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getstatus()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getusername()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getpassword()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getemail()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getuuid()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getname()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#arguments.user.getshortdescription()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#arguments.user.getdescription()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_BIT"  value="#arguments.user.getEditable()#" null="false" />
					)
				</cfquery>
				
				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
				SELECT MAX(iduser) AS id
				FROM "User"
				</cfquery>
				
				<cfset id = qry.id />
				
				<cfset arguments.user.setIdUser( id )/>
				
				<cfset setRole( arguments.user )/>
		
				<cfcatch type="any">
				
					<cftransaction action="rollback" />
					
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
				
				</cfcatch>
		
			</cftry>
			
		</cftransaction>

		<cfreturn id />
	</cffunction>
	
	<cffunction name="read" access="public" returntype="Query" output="false">
		<cfargument name="user" type="it.millemultimedia.easyBlog.model.User" required="true" />
		<cfset var qry="" />
		
		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT    	User_Role.idRole, "User".idUser, "User".status, "User".editable, 
						"User".username, "User".password, "User".email, "User".uuid, 
                      	"User".name, "User".shortdescription, "User".description
			FROM        "User" INNER JOIN
                      	User_Role ON "User".idUser = User_Role.idUser
			WHERE "User".iduser = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getidUser()#" null="false" />
		</cfquery>
		
		<cfreturn qry />
	</cffunction>
	
	<cffunction name="update" access="public" returntype="void" output="false">
		<cfargument name="user" type="it.millemultimedia.easyBlog.model.User" required="true" />
		
		<cfset var qry="" />
		
		<cftransaction>
	
			<cftry>

				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					UPDATE "User"
					SET status = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getstatus()#" null="false" />,
						username = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getusername()#" null="false" />,
						password = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getpassword()#" null="false" />,
						email = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getemail()#" null="false" />,
						name = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getname()#" null="false" />,
						shortdescription = <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#arguments.user.getshortdescription()#" null="false" />,
						description = <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#arguments.user.getdescription()#" null="false" />
					WHERE iduser = <cfqueryparam cfsqltype="CF_SQL_INTEGER"  value="#arguments.user.getIdUser()#" null="false" />
				</cfquery>
				
		
				<cfset setRole( arguments.user )/>
		
				<cfcatch type="any">
				
					<cftransaction action="rollback" />
					
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
				
				</cfcatch>
		
			</cftry>
			
		</cftransaction>

	</cffunction>
	
	<cffunction name="delete" access="public" returntype="void" output="false">
		<cfargument name="user" type="it.millemultimedia.easyBlog.model.User" required="true" />
		
		<cfset var qry="" />

		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			DELETE FROM "User"
			WHERE iduser = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getidUser()#" null="false" />
		</cfquery>
	</cffunction>
	
	
	<cffunction name="list" access="public" returntype="Query" output="false">
		<cfargument name="user" type="it.millemultimedia.easyBlog.model.User" required="true" />
		
		<cfset var qry="" />

		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT * 
			FROM "User"
			WHERE idUser is NOT NULL 
			<cfif !isNull( arguments.user.getidUser() ) >
			AND  idUser = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getidUser()#" null="false" />
			</cfif>
				
			<cfif !isNull( arguments.user.getstatus() ) >
			AND  status = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getstatus()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getusername() ) >
			AND  username = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getusername()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getpassword() ) >
			AND  password = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getpassword()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getemail() ) >
			AND  email = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getemail()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getuuid() ) >
			AND  uuid = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getuuid()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getname() ) >
			AND  name = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.user.getname()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getshortdescription() ) >
			AND  shortdescription = <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#arguments.user.getshortdescription()#" null="false" />
			</cfif>
			
				
			<cfif !isNull( arguments.user.getdescription() ) >
			AND  description = <cfqueryparam cfsqltype="CF_SQL_LONGVARCHAR" value="#arguments.user.getdescription()#" null="false" />
			</cfif>
			
		</cfquery>
		
		<cfreturn qry/>
	</cffunction>
	
	<cffunction name="getPermissions" access="public" output="false" returntype="Query">
		<cfargument name="idUser" type="String" required="true" />
		<cfset var qry="" />
		
		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT    Permission.name, Permission.description, Permission.id, User_Role.idUser
			FROM      Role INNER JOIN
                      Role_Permission ON Role.idRole = Role_Permission.idRole INNER JOIN
                      Permission ON Role_Permission.idPermission = Permission.idPermission INNER JOIN
                      User_Role ON Role.idRole = User_Role.idRole
			WHERE     User_Role.idUser IN( #arguments.idUser# )
		</cfquery>
		
		<cfreturn qry />
	</cffunction>
	
	<cffunction name="getPreferences" access="public" output="false" returntype="Query">
		<cfargument name="idUser" type="String" required="true" />
		<cfset var qry="" />
		
		<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
			SELECT    Preference.name, Preference.description, Preference.id, User_Role.idUser
			FROM      Role INNER JOIN
                      Role_Preference ON Role.idRole = Role_Preference.idRole INNER JOIN
                      User_Role ON Role.idRole = User_Role.idRole INNER JOIN
                      Preference ON Role_Preference.IdPreference = Preference.idPreference
			WHERE     User_Role.idUser IN( #arguments.idUser# )
		</cfquery>
		
		<cfreturn qry />
	</cffunction>
	
	<cffunction name="setRole" access="private" output="false" returntype="void">
		<cfargument name="user" type="it.millemultimedia.easyBlog.model.User" required="true" />
		<cfset var qry="" />
		
		<cftransaction>
		
			<cftry>

				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					DELETE User_Role
					WHERE idUser =	<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getidUser()#" null="false" />
				</cfquery>
				
				<cfquery name="qry" datasource="#getDataSource().getName()#" username="#getDataSource().getUserName()#" password="#getDataSource().getPassword()#">
					INSERT INTO User_Role
					(
						iduser,idrole
					)
					VALUES
					(
						<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getidUser()#" null="false" />,
					<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.user.getidRole()#" null="false" />
					)
				</cfquery>

				<cfcatch type="any" >
				
					<cftransaction action="rollback" />
					
					<cfthrow message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
				
				</cfcatch>			
			
			</cftry>
		
		</cftransaction>
	</cffunction>

</cfcomponent>
